
var api = require('../lib/clientApi');
var exception = require('../lib/exception');
var wx = require('../module/weixin');
var root = "http://m.fangmm.com";
var timeutil = require('../module/timeutil');

exports.getChief = function *() {

    log.http('chiefId: %s', this.params.chiefId);
    log.http('userId: %s', this.query.userId);

    var entityTeam = new api.forTeam();
    entityTeam.teamId = this.params.chiefId;
    entityTeam.userId = this.query.userId;

    var entityUser = new api.forUser();
    entityUser.userId = this.query.userId;

    var team = yield entityTeam.call();
    if(0 != team.code) {
        throw exception(exception.ServerError);
    }
    var entityHouse = new api.forHouse();
    entityHouse.houseId = team.team.houseId;

    var house = yield entityHouse.call();
    var user = yield entityUser.call();

    var jsapiSignature = yield wx.jsapiSignature.call(this, root + this.url);


    if(0 != user.code || 0 != house.code) {
        throw exception(exception.ServerError);
    }

    yield this.render('client/index', {title: '室长分享', user: user, team: team, house: house, jsapiSignature: jsapiSignature});
}


exports.getRoom = function *() {

    log.http('houseId: %s', this.params.houseId);
    var entityHouse = new api.forHouse();
    entityHouse.houseId = this.params.houseId;

    var entityGroup = new api.forGroup();
    entityGroup.houseId = this.params.houseId;

    var entityFavorite = new api.forFavorite();
    entityFavorite.houseId = this.params.houseId;

    var entityUser = new api.forUser();
    entityUser.userId = this.query.userId;

    var user = yield entityUser.call();

    var house = yield entityHouse.call();
    house.house.rooms.filter(function(room) {
        var bool = false;
        house.house.images.filter(function(image) {
            if(image.classify == room.name) {
                bool = true;
            }
        })
        if(!bool) {
            house.house.images.push({classify: room.name, path: "http://img.fangmm.com/default.jpg:"})
        }
    });

    var jsapiSignature = yield wx.jsapiSignature.call(this, root + this.url);
    var group = yield entityGroup.call();
    var favorite = yield entityFavorite.call();
    if(0 != house.code || 0 != group.code || 0 != favorite.code) {
        throw exception(exception.ServerError);
    }

    yield this.render('client/roomDetail', {title: '房源详情',user: user, house: house, group: group, favorite: favorite, jsapiSignature: jsapiSignature});
}

exports.getUser = function *() {

    log.http('userId: %s', this.params.userId);
    var entityUser = new api.forUser();
    entityUser.userId = this.params.userId;

    var entityUserTeam = new api.forUserTeam();
    entityUserTeam.userId = this.params.userId;

    var user = yield entityUser.call();
    var userTeam = yield entityUserTeam.call();

    console.log('------', root + this.url);
    var jsapiSignature = yield wx.jsapiSignature.call(this, root + this.url);

    if(0 != user.code) {
        throw exception(exception.ServerError);
    }
    yield this.render('client/user', {title: '个人详情', user: user, userTeam: userTeam, jsapiSignature: jsapiSignature});
}

exports.getCommunity = function *() {

    log.http('communityId: %s', this.params.communityId);

    var entityCommunity = new api.forCommunity();
    entityCommunity.communityId = this.params.communityId;

    var entitySurrounding = new api.forSurrounding();
    entitySurrounding.communityId = this.params.communityId;

    var entityHouseList = new api.forHouseList();
    entityHouseList.communityId = this.params.communityId;

    var community = yield entityCommunity.call();
    var surrounding = yield entitySurrounding.call();
    var houseList = yield entityHouseList.call();

    var jsapiSignature = yield wx.jsapiSignature.call(this, root + this.url);
    if(0 != community.code || 0 != surrounding.code || 0 != houseList.code) {
        throw exception(exception.ServerError);
    }

    var self = this;
    houseList = houseList.houses.filter(function(house) {
        return house.community != self.params.communityId;
    })

    yield this.render('client/community', {title: '小区详情', community: community, surrounding: surrounding ,houseList: houseList, jsapiSignature: jsapiSignature})
}

exports.publishRoom = function *() {

    yield this.render('client/publish', {title: '发布房源'});
}

exports.postRoom = function *() {

    console.log(this.request.body);
    console.log(this.query.userId);
    var img = null;
    if(Object.keys(this.request.body.files).length) {

        var entityImg = new api.forHouseImg();
        img = yield entityImg.call(this.request.body.files);
        console.log(img);
    }

    var entityPostHouse = new api.forPostHouse();
    var self = this;
    Object.keys(this.request.body.fields).forEach(function(param) {
        entityPostHouse[param] = self.request.body.fields[param];
    });
    entityPostHouse.userId = this.query.userId;
    if(img) {
        img.resources.forEach(function(resource) {
            entityPostHouse.fileIds.push(resource.uuid);
        })
    }
    var post = yield entityPostHouse.call();
    this.body = JSON.stringify(post);

}

exports.getPanoramic = function *() {

    log.http('panoramicId: %s', this.params.panoramicId);

    var entityPanoramic = new api.forPanoramic();
    entityPanoramic.houseId = this.params.panoramicId;

    var panoramic = yield entityPanoramic.call();
    console.log(JSON.stringify(panoramic));

    yield this.render('client/panoramic', {title: '全景看房', panoramic: panoramic})
}

exports.getFangmm = function *() {
    yield this.render('client/about-fangmm', {title: '房妈妈'})
}

exports.getChies = function *() {
    yield this.render('client/about-chief', {title: '室长简介'})
}

exports.getGuide = function *() {
    yield this.render('client/about-guide', {title: '用户指南'})
}

exports.getProvision = function *() {
    yield this.render('client/about-provision', {title: '法律条规'})
}

exports.getFeed = function *() {
    yield this.render('client/feedback', {title: '用户反馈'})
}

// weixin3.0
exports.getList = function *() {

    var entityAdd = new api.forAdd();
    entityAdd.classify = 'index';

    var add = yield entityAdd.call();
    yield this.render('weixin/index', {title: '房妈妈', add: add})
}

exports.getDetail = function *() {

    var listId = this.params.listId;
    var jsapiSignature = yield wx.jsapiSignature.call(this, root + this.url);

    yield this.render('weixin/list', {title: '房源详情', listId: listId, jsapiSignature: jsapiSignature});
}

exports.getRequirement = function *() {
    console.log(this.session.User.userId, this.session.token);
    var bool = false;
    var requirementEntity = new api.forRequirement();
    requirementEntity.userId = this.session.User.userId;
    requirementEntity.token = this.session.token;
    var requirement = yield requirementEntity.call();

    if(0 != requirement.code) {
        throw exception(exception.ServerError);
    }

    if(!requirement.requirement.day) {
        bool = true;
    }
    yield this.render('weixin/requirement', {title: '租房需求', requirement: requirement, token: this.session.token,userId: this.session.User.userId, bool: bool})
}

exports.getRegister = function *() {

    yield this.render('weixin/register', {title: '登入/注册'})
}
exports.getCode = function *() {

    if(!this.query.mobile) {
        return (this.body = 0);
    }
    var lastDate = this.session.lastDate;
    if(lastDate && timeutil.getTimeStamp() - lastDate <= 60) {
        return (this.body = -1);
    }

    this.session.lastDate = timeutil.getTimeStamp();

    var codeEntity = new api.forCode();
    codeEntity.mobile = this.query.mobile;
    var code = yield codeEntity.call();

    if(code.code != 0) {
        return (this.body = -2);
    }

    this.body = 1;

}

exports.getAuth = function *() {

    console.log('auth',this.request.body );
    var authEntity = new api.forLogin();

    var self = this;
    Object.keys(this.request.body).forEach(function(param) {
        authEntity[param] = self.request.body[param];
    });

    var auth = yield authEntity.call();
    if(auth.code == 0) {
        this.session.User = auth.user;
        this.session.token = auth.token;
        if(this.session.refer) {
            this.redirect(this.session.refer)
        } else {
            this.redirect('/');
        }
    } else {
        throw exception(exception.ServerError);
    }
}

